package com.weng.platform.minder.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.cloud.utils.CloudAuthUtil;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.minder.entity.MinderInfo;
import com.weng.platform.minder.model.vo.MinderInfoVo;
import com.weng.platform.minder.service.IMinderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;


/**
 * 思维导图信息 前端控制器
 *
 * @author wenghui
 * @date 2020-07-17 20:53:59
 *
 */  
@Slf4j
@RestController
@RequestMapping("/minder-info")
@Api(value = "思维导图信息api", tags = {"思维导图信息操作接口"})
public class MinderInfoApi extends BaseController {
    @Autowired
    private IMinderInfoService minderInfoService;

    /**
     * 功能描述:单个获取思维导图信息
     */
    @ApiOperation(value = "获取思维导图信息", notes = "")
    @GetMapping(value = "/get")
    public ResponseData get(String id){
        MinderInfo minderInfo = minderInfoService.getById(id);
        return success(minderInfo);
    }


    /**
     * 条件 分页查询思维导图信息
     */
    @ApiOperation(value = "分页获取思维导图信息", notes = "")
    @GetMapping(value = "/list")
    public ResponseData list(Integer pageNo, Integer pageSize , MinderInfo minderInfo, Boolean isQueryChild, HttpServletRequest request){
				String ownerId = CloudAuthUtil.getCurUserId(request);
				IPage<MinderInfo> iPage = minderInfoService.list(pageNo, pageSize ,minderInfo,ownerId, isQueryChild);
        return success(iPage);
    }


    /**
	 * 新增思维导图信息
	 */
	@ApiOperation(value = "新增思维导图信息", notes = "")
	@PostMapping(value = "/add")
	public ResponseData add(@Valid @RequestBody MinderInfoVo minderInfoReq, HttpServletRequest request){
		MinderInfo minderInfo = Convert.convert(MinderInfo.class, minderInfoReq);
		String userId = CloudAuthUtil.getCurUserId(request);
		String userName = CloudAuthUtil.getCurUserTrueName(request);
		minderInfo.setOwnerId(userId);
		minderInfo.setOwnerName(userName);
		minderInfoService.add(minderInfo);
		return success(minderInfo);
	}

	/**
	 * 更新思维导图信息
	 */
	@ApiOperation(value = "更新思维导图信息", notes = "")
	@PostMapping(value = "/update")
	public ResponseData update(@Valid @RequestBody MinderInfoVo minderInfoReq){
		MinderInfo minderInfo = Convert.convert(MinderInfo.class, minderInfoReq);
		minderInfoService.update(minderInfo);
		return success(minderInfo);
	}


	/**
	 * 删除思维导图信息
	 */
	@ApiOperation(value = "ids 删除思维导图信息", notes = "可以删除多个，中间以,号分隔")
	@PostMapping(value = "/delete")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		boolean flag = minderInfoService.removeByIds(Arrays.asList(ids.split(",")));
		if(flag){
			return success(deleteReq.getIds());
		}else{
			return failed("删除失败");
		}
	}
}
